package com.cskaoyan.market.service.admin.impl;

import com.cskaoyan.market.db.domain.MarketSystem;
import com.cskaoyan.market.db.domain.MarketSystemExample;
import com.cskaoyan.market.db.mapper.MarketSystemMapper;
import com.cskaoyan.market.service.admin.MarketConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class
MarketConfigServiceImpl implements MarketConfigService {

    @Autowired
    MarketSystemMapper systemMapper;

    /**
     * @MethodName getMarketSystemObj
     * @Description 提取修改对应对象的方法，传入keyValue数据，
     * @param: keyValue
     * @return: com.cskaoyan.market.db.domain.MarketSystem  返回的对象为封装完成的修改数据对象
     * @Author M
     * @Date 22:09 2023/5/8
     **/
    private static MarketSystem getMarketSystemObj(String keyValue) {
        // 创建一个marketSystem对象,对market_express_freight_min进行修改
        MarketSystem freightMinConfig = new MarketSystem();
        freightMinConfig.setKeyValue(keyValue);
        // TODO: 此处可以判断一下addTime是否为空来决定是否修改AddTime
        freightMinConfig.setAddTime(LocalDateTime.now());
        freightMinConfig.setUpdateTime(LocalDateTime.now());
        return freightMinConfig;
    }

    @Override
    public String getKeyValueByKeyName(String keyName) {

        MarketSystemExample systemExample = new MarketSystemExample();
        systemExample.or().andDeletedEqualTo(false).andKeyNameEqualTo(keyName);
        MarketSystem marketSystemObj = systemMapper.selectOneByExample(systemExample);

        return marketSystemObj.getKeyValue();
    }

    @Override
    public int setExpressConfig(String freightMin, String freightValue) {

        MarketSystem freightMinConfig = getMarketSystemObj(freightMin);
        MarketSystemExample freightMinExample = new MarketSystemExample();
        freightMinExample.or().andKeyNameEqualTo("market_express_freight_min").andDeletedEqualTo(false);
        int row1 = systemMapper.updateByExampleSelective(freightMinConfig, freightMinExample);

        // 创建一个marketSystem对象,对"market_express_freight_value"进行修改
        MarketSystem freightValueConfig = getMarketSystemObj(freightValue);
        MarketSystemExample freightValueExample = new MarketSystemExample();
        freightValueExample.or().andKeyNameEqualTo("market_express_freight_value").andDeletedEqualTo(false);
        int row2 = systemMapper.updateByExampleSelective(freightValueConfig, freightValueExample);

        // 当两次查询都成功时返回1，否则报错
        if (row1 != 1 || row2 != 1) {
            return 0;
        }
        return 1;
    }


    /*
            "market_order_unconfirm": "8",
	        "market_order_unpaid": "31",
	        "market_order_comment": "8"
     */
    @Override
    public int setOrderConfig(String unconfirm, String unpaid, String comment) {

        // 创建一个order_unconfirm对象,对market_order_unconfirm进行修改
        MarketSystem unconfirmConfig = getMarketSystemObj(unconfirm);
        MarketSystemExample unconfirmExample = new MarketSystemExample();
        unconfirmExample.or().andKeyNameEqualTo("market_order_unconfirm").andDeletedEqualTo(false);
        int row1 = systemMapper.updateByExampleSelective(unconfirmConfig, unconfirmExample);

        // 创建一个order_unpaid对象,对"market_order_unpaid"进行修改
        MarketSystem unpaidConfig = getMarketSystemObj(unpaid);
        MarketSystemExample unpaidExample = new MarketSystemExample();
        unpaidExample.or().andKeyNameEqualTo("market_order_unpaid").andDeletedEqualTo(false);
        int row2 = systemMapper.updateByExampleSelective(unpaidConfig, unpaidExample);

        // 创建一个order_comment对象,对market_order_comment进行修改
        MarketSystem commentConfig = getMarketSystemObj(comment);
        MarketSystemExample commentExample = new MarketSystemExample();
        commentExample.or().andKeyNameEqualTo("market_order_comment").andDeletedEqualTo(false);
        int row3 = systemMapper.updateByExampleSelective(commentConfig, commentExample);

        // 当三次查询都成功时返回1，否则报错
        if (row1 != 1 || row2 != 1 || row3 != 1) {
            return 0;
        }
        return 1;
    }

    @Override
    public int setWxConfig(String indexNew, String indexTopic, String share, String indexBrand, String catlogGoods, String catlogList, String indexHot) {

        // indexNew
        MarketSystem indexNewConfig = getMarketSystemObj(indexNew);
        MarketSystemExample indexNewExample = new MarketSystemExample();
        indexNewExample.or().andKeyNameEqualTo("market_wx_index_new").andDeletedEqualTo(false);
        int row1 = systemMapper.updateByExampleSelective(indexNewConfig, indexNewExample);

        // indexTopic
        MarketSystem indexTopicConfig = getMarketSystemObj(indexTopic);
        MarketSystemExample indexTopicExample = new MarketSystemExample();
        indexTopicExample.or().andKeyNameEqualTo("market_wx_index_topic").andDeletedEqualTo(false);
        int row2 = systemMapper.updateByExampleSelective(indexTopicConfig, indexTopicExample);

        // share
        MarketSystem shareConfig = getMarketSystemObj(share);
        MarketSystemExample shareExample = new MarketSystemExample();
        shareExample.or().andKeyNameEqualTo("market_wx_share").andDeletedEqualTo(false);
        int row3 = systemMapper.updateByExampleSelective(shareConfig, shareExample);

        // indexBrand
        MarketSystem indexBrandConfig = getMarketSystemObj(indexBrand);
        MarketSystemExample indexBrandExample = new MarketSystemExample();
        indexBrandExample.or().andKeyNameEqualTo("market_wx_index_brand").andDeletedEqualTo(false);
        int row4 = systemMapper.updateByExampleSelective(indexBrandConfig, indexBrandExample);

        // catlogGoods
        MarketSystem catlogGoodsConfig = getMarketSystemObj(catlogGoods);
        MarketSystemExample catlogGoodsExample = new MarketSystemExample();
        catlogGoodsExample.or().andKeyNameEqualTo("market_wx_catlog_goods").andDeletedEqualTo(false);
        int row5 = systemMapper.updateByExampleSelective(catlogGoodsConfig, catlogGoodsExample);

        // catlogList
        MarketSystem catlogListConfig = getMarketSystemObj(catlogList);
        MarketSystemExample catlogListExample = new MarketSystemExample();
        catlogListExample.or().andKeyNameEqualTo("market_wx_catlog_list").andDeletedEqualTo(false);
        int row6 = systemMapper.updateByExampleSelective(catlogListConfig, catlogListExample);

        // indexHot
        MarketSystem indexHotConfig = getMarketSystemObj(indexHot);
        MarketSystemExample indexHotExample = new MarketSystemExample();
        indexHotExample.or().andKeyNameEqualTo("market_wx_index_hot").andDeletedEqualTo(false);
        int row7 = systemMapper.updateByExampleSelective(indexHotConfig, indexHotExample);

        // 当三次查询都成功时返回1，否则报错
        if (row1 != 1 || row2 != 1 || row3 != 1 || row4 != 1 || row5 != 1 || row6 != 1 || row7 != 1) {
            return 0;
        }
        return 1;
    }

    @Override
    public int setMallConfig(String longitude, String latitude, String address, String qq, String phone, String name) {

        // longitude
        MarketSystem longitudeConfig = getMarketSystemObj(longitude);
        MarketSystemExample longitudeExample = new MarketSystemExample();
        longitudeExample.or().andKeyNameEqualTo("market_mall_longitude").andDeletedEqualTo(false);
        int row1 = systemMapper.updateByExampleSelective(longitudeConfig, longitudeExample);

        // latitude
        MarketSystem latitudeConfig = getMarketSystemObj(latitude);
        MarketSystemExample latitudeExample = new MarketSystemExample();
        latitudeExample.or().andKeyNameEqualTo("market_mall_latitude").andDeletedEqualTo(false);
        int row2 = systemMapper.updateByExampleSelective(latitudeConfig, latitudeExample);

        // address
        MarketSystem addressConfig = getMarketSystemObj(address);
        MarketSystemExample addressExample = new MarketSystemExample();
        addressExample.or().andKeyNameEqualTo("market_mall_address").andDeletedEqualTo(false);
        int row3 = systemMapper.updateByExampleSelective(addressConfig, addressExample);

        // qq
        MarketSystem qqConfig = getMarketSystemObj(qq);
        MarketSystemExample qqExample = new MarketSystemExample();
        qqExample.or().andKeyNameEqualTo("market_mall_qq").andDeletedEqualTo(false);
        int row4 = systemMapper.updateByExampleSelective(qqConfig, qqExample);

        // phone
        MarketSystem phoneConfig = getMarketSystemObj(phone);
        MarketSystemExample phoneExample = new MarketSystemExample();
        phoneExample.or().andKeyNameEqualTo("market_mall_phone").andDeletedEqualTo(false);
        int row5 = systemMapper.updateByExampleSelective(phoneConfig, phoneExample);

        // name
        MarketSystem nameConfig = getMarketSystemObj(name);
        MarketSystemExample nameExample = new MarketSystemExample();
        nameExample.or().andKeyNameEqualTo("market_mall_name").andDeletedEqualTo(false);
        int row6 = systemMapper.updateByExampleSelective(nameConfig, nameExample);

        // 当三次查询都成功时返回1，否则报错
        if (row1 != 1 || row2 != 1 || row3 != 1 || row4 != 1 || row5 != 1 || row6 != 1) {
            return 0;
        }
        return 1;
    }
}
